/** 
 ** Copyright (c) 2010 Ushahidi Inc
 ** All rights reserved
 ** Contact: team@ushahidi.com
 ** Website: http://www.ushahidi.com
 ** 
 ** GNU Lesser General Public License Usage
 ** This file may be used under the terms of the GNU Lesser
 ** General Public License version 3 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.LGPL included in the
 ** packaging of this file. Please review the following information to
 ** ensure the GNU Lesser General Public License version 3 requirements
 ** will be met: http://www.gnu.org/licenses/lgpl.html.	
 **	
 **
 ** If you have questions regarding the use of this file, please contact
 ** Ushahidi developers at team@ushahidi.com.
 ** 
 **/

package com.ushahidi.android.app.net;

import java.io.IOException;

import org.apache.http.HttpResponse;

import android.util.Log;

import com.ushahidi.android.app.UshahidiPref;

public class Categories {
	
    	private final static String incidentCateArg = "&by=cattype&category_type=1";
    	private final static String resourceCateArg = "&by=cattype&category_type=2";
    	    	    
	public static boolean getAllCategoriesFromWeb() throws IOException 
	{
	    StringBuilder uriBuilder = new StringBuilder( UshahidiPref.domain);
	    if(!UshahidiPref.domain.endsWith("/"))
		uriBuilder.append("/");
	    uriBuilder.append("api?task=categories&resp=xml");
	    //modified by OceanMaster, fetch categories seperately
	    return 
	    	(getIncidentCategoriesFromWeb(uriBuilder.toString()+incidentCateArg))
	    		&&
		(getResourceCategoriesFromWeb(uriBuilder.toString()+resourceCateArg));
	}
	//Added by OceanMaster
	public static boolean getIncidentCategoriesFromWeb(String url) throws IOException 
	{
	    String categories = "";
	    
	    HttpResponse response = UshahidiHttpClient.GetURL( url);
	    Log.v("Oceanmaster", "Request:["+url+"]");
	    
	    if( response == null ) {
		return false;
	    }
		
	    final int statusCode = response.getStatusLine().getStatusCode();
		
	    if( statusCode == 200 ) {
		categories = UshahidiHttpClient.GetText(response).toString();
		Log.v("Oceanmaster", "Incident Categories Result:["+categories+"]");
		UshahidiPref.incidentCategoriesResponse = categories;
		return true;
	    } else {
		return false;
	    }
	}
	
	//Added by OceanMaster
	public static boolean getResourceCategoriesFromWeb(String url) throws IOException 
	{
	    String categories = "";
	    
	    HttpResponse response = UshahidiHttpClient.GetURL( url);
	    Log.v("Oceanmaster", "Request:["+url+"]");
	    
	    if( response == null ) {
		return false;
	    }
		
	    final int statusCode = response.getStatusLine().getStatusCode();
		
	    if( statusCode == 200 ) {
		categories = UshahidiHttpClient.GetText(response).toString();
		Log.v("Oceanmaster", "Resource Categories Result:["+categories+"]");
		UshahidiPref.resourceCategoriesResponse = categories;
		return true;
	    } else {
		return false;
	    }
	}
}
